<?php namespace Admin\Controller;
//货品列表控制器
class GoodsListController extends CommonController{
	protected $model;
	public function __auto(){
		$this->model=new \Common\Model\GoodsList;
	}
	
	public function index(){
		$gid=Q('get.gid',0,'intval');
		$goodsAttModel=new \Common\Model\GoodsAtt;
		$data=$goodsAttModel
				->join('typeattribute','taid','=','typeattribute_tid')
				->where("goods_gid={$gid} AND choose=1")
				->groupBy('typeattribute_tid')
				->get();
//		循环$data拿出tvalue	只拿当前商品选择过的类型属性
		foreach($data as $k=>$v){
			$data[$k]['value']=$goodsAttModel->where("typeattribute_tid={$v['taid']} AND  goods_gid={$gid}")->get();
		};
//	p($data);exit;
//		显示已有的货品
		$goodsListModel=new \Common\Model\GoodsList;
		$listData=$goodsListModel->where("goods_gid={$gid}")->get();
		foreach($listData as $k=>$v){
			$listData[$k]['value']=$goodsAttModel->where("gaid IN({$v['combine']})")->get();
		}
//		p($listData);exit;
//		添加
	if(IS_POST){
//		p(Q('post.'));exit;
		if($this->model->store()){
			View::success('添加成功');
		}
		}
		
		View::with('listData',$listData);
		View::with('data',$data);
		View::make();
	}
	
//	编辑
	public function edit(){
		$gid=Q('get.gid',0,'intval');
//		p($gid);exit;
//		获得下拉菜单的数据
		$goodsAttModel=new \Common\Model\GoodsAtt;
		$data=$goodsAttModel
				->join('typeattribute','taid','=','typeattribute_tid')
				->where("goods_gid={$gid} AND choose=1")
				->groupBy('typeattribute_tid')
				->get();
//		循环$data拿出tvalue	只拿当前商品选择过的类型属性
		foreach($data as $k=>$v){
			$data[$k]['value']=$goodsAttModel->where("typeattribute_tid={$v['taid']} AND  goods_gid={$gid}")->get();
		};
//		p($data);exit;
//		获得旧数据
		$glid=Q('get.glid',0,'intval');
		$oldData=$this->model->where("glid={$glid}")->find();
//		p($oldData);
		$gaid=explode(',', $oldData['combine']);
//		p($id);
		View::with('gaid',$gaid);
		View::with('oldData',$oldData);
		View::with('data',$data);
		View::make();
	}
	
//	删除
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		View::success('删除成功');
		
	}
	

}
